﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Temp.Model;
using Temp.Database;
using Microsoft.Xna.Framework;
using Temp.NLG;
namespace Temp.Controller
{
    class ObjectController
    {
        private SQLiteFactory sqlf; 
        private SQLite.SQLiteConnection conn;
        List<Temp.Model.Object> objs;
        private int curPlace;
        PlaceController pCtr;
        private Random rand;

        public ObjectController() {
            sqlf = new SQLiteFactory();
            pCtr = new PlaceController();
            rand  = new Random();
        }
        
        public int CurPlace
        {
            get { return curPlace; }
            set { curPlace = value; }
        }

        public List<String> getDescription(String objtName)
        {
            List<String> descs = new List<String>();
            List<Ontology> onts = new List<Ontology>();
            List<MessageTemplate> msg = new List<MessageTemplate>();
            TemplateDecoder tDec = new TemplateDecoder();
  
            String str = "", msgTemp="";
            conn = sqlf.getConnection();
            try
            {
                onts = conn.Query<Ontology>("SELECT * from Object_Ont WHERE relation !='locatedAt' and concept1='" + objtName + "'").ToList();
                msg = conn.Query<MessageTemplate>("SELECT * from MessageTemplate where type='forObj'").ToList();
                
                foreach (Ontology ont in onts)
                { 
                    msgTemp=msg[rand.Next(msg.Count)].message;
                    str=tDec.getMessage(msgTemp, ont);
                    System.Diagnostics.Debug.WriteLine(str);
                    descs.Add(str);
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Error: no dialogue/ obj does not exist.");
            }
            conn.Close();
            
            return descs;
        }

        

        public List<Temp.Model.Object> getObjectsInPlace(int curPlace)
        {
            objs = new List<Temp.Model.Object>();
            List<Ontology> onts = new List<Ontology>();     
            List<String> objectNames = new List<String>();
            List<Temp.Model.Object> objs2 = new List<Temp.Model.Object>();

            conn = sqlf.getConnection();
            objs2 = conn.Query<Temp.Model.Object>("SELECT * from Object WHERE idPlace="+curPlace ).ToList();
            foreach (Temp.Model.Object o in objs2)
                objs.Add(o);
            
            conn.Close();
            
            return objs;
        }
        
    }
}
